package com.wcgksoft.operation;

import com.wcgksoft.entity.Alert.Alarm;
import com.wcgksoft.entity.ZData.*;
import com.wcgksoft.entity.dataAnalysis.DepartProjectData;
import com.wcgksoft.entity.dataAnalysis.Threshold;
import com.wcgksoft.service.BasicDataService;
import com.wcgksoft.service.DataAnalysis.DepartProjectDataService;
import com.wcgksoft.service.DataAnalysis.ThresholdService;

import com.wcgksoft.service.alarm.AlarmService;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.context.ServletContextAware;

import javax.servlet.ServletContext;
import java.util.Calendar;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

/**
 * 天气情况
 * 抓取温度（如果湿度也能抓取好），每天抓取5个时间的温度（早上8点，上午10点，中午12点，下午2点，傍晚4点，傍晚6点），并存到数据库中。
 *
 * @author 蒋世维
 */
public class alarmEml implements InitializingBean, ServletContextAware {

  @Autowired
  private ThresholdService thresholdService;

  @Autowired
  private AlarmService alarmService;

  @Autowired
  private BasicDataService basicDataServise;

  private Boolean Onred(Double s1, Double s2, String ors) {
    if (ors.equals(">")) {
      return s1 > s2;
    } else if (ors.equals("<")) {
      return s1 < s2;
    } else if (ors.equals(">=")) {
      return s1 >= s2;
    } else if (ors.equals("<=")) {
      return s1 <= s2;
    } else if (ors.equals("==")) {
      return s1 == s2;
    } else {
      return false;
    }
  }

  public void afterPropertiesSet() throws Exception {
    long timeInterval = 30 * 60 * 100;// 1小时的毫秒设定
    Calendar c = Calendar.getInstance();//可以对每个时间域单独修改
    Timer timer = new Timer();
    timer.schedule(new TimerTask() {
      public void run() {

        DetachedCriteria criteria = DetachedCriteria.forClass(Threshold.class);
        List<Threshold> all = thresholdService.findByConditions(criteria);
        for (Threshold threshold : all) {
          DepartProjectData departProjectData = threshold.getDepartProjectData();
          Alarm alarm;

            DetachedCriteria dataTemperature = DetachedCriteria.forClass(BasicData.class);
            dataTemperature.add(Restrictions.eq("datatype", departProjectData.getDataType().getD_key()));
            dataTemperature.add(Restrictions.eq("no", departProjectData.getEquipment().getE_number()));
            dataTemperature.add(Restrictions.eq("com",departProjectData.getCom()));
            List<BasicData> ztList = basicDataServise.findByConditions(dataTemperature).subList(0, 2);

            DetachedCriteria alarms = DetachedCriteria.forClass(Alarm.class);
            criteria.createAlias("departProjectData", "departProjectData");
            alarms.add(Restrictions.eq("departProjectData.id", departProjectData.getId()));
            List<Alarm> all1 = alarmService.findByConditions(alarms);
            if (all1.size() == 0) {
              alarm = new Alarm();
            } else {
              alarm = all1.get(0);
            }
            if (ztList.stream().allMatch(element -> Onred(Double.valueOf(element.getData()), threshold.getGjfw(), threshold.getGjfwyz()))) {
              alarm.setAlarmtime(ztList.get(0).getAcquisitiontime()+"");
              alarm.setTypecode(departProjectData.getDataType().getD_key());
              alarm.setArname(departProjectData.getDataType().getD_name());
              alarm.setCurrentvalue(Double.valueOf(ztList.get(0).getData()));
              alarm.setNo(departProjectData.getEquipment().getE_number());
              alarm.setCom(departProjectData.getCom());
              alarm.setLevel("高级");
              alarm.setDepartProjectData(departProjectData);
              alarm.setSetvalue(threshold.getGjfw());
//              alarm.setStitas(1);
              alarmService.saveOrUpdate(alarm);
            } else if (ztList.stream().allMatch(element -> Onred(Double.valueOf(element.getData()), threshold.getQwcb(), threshold.getQwcbyz()))) {
              alarm.setDepartProjectData(departProjectData);
              alarm.setAlarmtime(ztList.get(0).getAcquisitiontime()+"");
              alarm.setArname(departProjectData.getDataType().getD_name());
              alarm.setCurrentvalue(Double.valueOf(ztList.get(0).getData()));
              alarm.setLevel("中级");
              alarm.setNo(departProjectData.getEquipment().getE_number());
              alarm.setCom(departProjectData.getCom());
              alarm.setSetvalue(threshold.getGjfw());
              alarmService.saveOrUpdate(alarm);
            } else if (ztList.stream().allMatch(element -> Onred(Double.valueOf(element.getData()), threshold.getZcfw(), threshold.getZcfwyz()))) {
              alarm.setDepartProjectData(departProjectData);
              alarm.setAlarmtime(ztList.get(0).getAcquisitiontime()+"");
              alarm.setArname(departProjectData.getDataType().getD_name());
              alarm.setCurrentvalue(Double.valueOf(ztList.get(0).getData()));
              alarm.setLevel("正常");
              alarm.setNo(departProjectData.getEquipment().getE_number());
              alarm.setCom(departProjectData.getCom());
              alarm.setSetvalue(threshold.getGjfw());
              alarmService.saveOrUpdate(alarm);
            } else {
              System.out.println("正常");
            }



        }
      }
    }, 15000, timeInterval);//定的定时器在15:10分开始执行,每隔 1小时执行一次.

  }

  @Override
  public void setServletContext(ServletContext arg0) {


  }


}
